import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class AutocompleteDemo extends StatelessWidget {
  const AutocompleteDemo({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("AutocompleteDemo"),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Text(
              "Type below to autocomplete the following possible results: ${AutoCompleteExample._kOptions}"),
          const AutoCompleteExample()
        ],
      ),
    );
  }
}

class AutoCompleteExample extends StatelessWidget {
  static const List<String> _kOptions = <String>[
    'aardvark',
    'bobcat',
    'chameleon'
  ];

  const AutoCompleteExample({super.key});

  @override
  Widget build(BuildContext context) {
    return Autocomplete(
      optionsBuilder: (TextEditingValue textEditingValue) {
        if (textEditingValue.text == '') {
          return const Iterable<String>.empty();
        }
        return _kOptions.where((String element) {
          return element.contains(textEditingValue.text.toLowerCase());
        });
      },
      onSelected: (String selection) {
        debugPrint('You just selected $selection');
      },
    );
  }
}
